﻿/*

文件名称:MapCommon.js
文件描述:高德地图收费版
设计作者:王敏
编码作者:王敏
完成日期:2015-8-20

*/
var urlParam = document.URL;
var uRl = (urlParam.slice(7,21));
if(uRl === "www.yczhhb.com"){
    document.write('<script language="javascript" src="http://www.yinchuanmap.com:9081/apis?t=javascriptmap&v=3.1.2&key=6a82883d450b31cb0b3577e98ff6d1246dbef63a7224f997fc148baa0b7f81c1eda6fcc3fd003db0"></script>');
}
else{
	document.write('<script language="javascript" src="http://app.mapabc.com/apis?t=javascriptmap&v=3&key=b0a7db0b3a30f944a21c3682064dc70ef5b738b062f6479a5eca39725798b1ee300bd8d5de3a4ae3"></script>');

    //document.write('<script language="javascript" src="http://10.113.10.121:8081/apis?t=javascriptmap&v=3.1.2&key=f08cc6fd6bfa9b74607ea50452caf8e1df3754d27224f997fc148baa0b7f81c1eda6fcc3fd003db0"></script>');
}

var DEF_PI = Math.PI; // 3.14159265359
var DEF_2PI = 2 * Math.PI; // 6.28318530712
var DEF_PI180 = Math.PI / 180.0; // 0.01745329252
var DEF_R = 6370996.81; // radius of earth
var circle;


// 利用弧夹角方式计算地图上两点之间的距离，和百度地图计算结果不一致
function GetShortDistance(lon1, lat1, lon2, lat2) {
    var ew1, ns1, ew2, ns2;
    var dx, dy, dew;
    var distance;
    // 角度转换为弧度
    ew1 = lon1 * DEF_PI180;
    ns1 = lat1 * DEF_PI180;
    ew2 = lon2 * DEF_PI180;
    ns2 = lat2 * DEF_PI180;
    // 经度差
    dew = ew1 - ew2;
    // 若跨东经和西经180 度，进行调整
    if (dew > DEF_PI)
        dew = DEF_2PI - dew;
    else if (dew < -DEF_PI)
        dew = DEF_2PI + dew;
    dx = DEF_R * Math.cos(ns1) * dew; // 东西方向长度(在纬度圈上的投影长度)
    dy = DEF_R * (ns1 - ns2); // 南北方向长度(在经度圈上的投影长度)
    // 勾股定理求斜边长
    distance = Math.sqrt(dx * dx + dy * dy);
    return distance;
}

// 利用弧夹角方式计算地图上两点之间的距离，与百度方式一致
function GetLongDistance(lon1, lat1, lon2, lat2) {
    var ew1, ns1, ew2, ns2;
    var distance;
    // 角度转换为弧度
    ew1 = lon1 * DEF_PI180;
    ns1 = lat1 * DEF_PI180;
    ew2 = lon2 * DEF_PI180;
    ns2 = lat2 * DEF_PI180;
    // 求大圆劣弧与球心所夹的角(弧度)
    distance = Math.sin(ns1) * Math.sin(ns2) + Math.cos(ns1) * Math.cos(ns2) * Math.cos(ew1 - ew2);
    // 调整到[-1..1]范围内，避免溢出
    if (distance > 1.0)
        distance = 1.0;
    else if (distance < -1.0)
        distance = -1.0;
    // 求大圆劣弧长度
    distance = DEF_R * Math.acos(distance);
    return distance;
}

//----初始化高德地图------
//mapId:显示地图的DIV,string
//bScale:地图比例尺控件,bool
//bTool:地图缩放工具控件,bool
//bOverView:地图鹰眼工具控件,bool
var map;
var mouseTool;

function mapInit(mapId, lng, lat, zoom, bScale, bTool, bOverView, bMouseTool) {
    if (lng === 0 || lat === 0 || lng === null || lat === null) {
        map = new MMap.Map(mapId);
    } else {
        var options = {
            level: zoom,
            center: new MMap.LngLat(lng, lat)
        };
        map = new MMap.Map(mapId, options); //创建地图
    }
    setMapEvent(map); //设置地图事件
    addMMapControl(map, bScale, bTool, bOverView, bMouseTool); //向地图添加控件
    if (bMouseTool) {
        InitMouseTool();
    }
    return map;
};

//----地图事件设置函数---
function setMapEvent(map) {
    map.setStatus({
        dragEnable: true
    });
    map.setStatus({
        keyboardEnable: true
    });
    map.setStatus({
        doubleClickZoom: false
    });
}

//---地图控件---
function addMMapControl(map, bScale, bToolBar, bOverView, bMouseTool) {
    if (map === null || map === undefined) {
    	return;
    }
    //显示地图缩放工具插件
    if (bMouseTool) {
        map.plugin(["MMap.ToolBar"], function() {
            var toolbar = new MMap.ToolBar();
            map.addControl(toolbar);
        })
    }
    //显示地图比例尺
    if (bScale) {
        map.plugin(["MMap.Scale"], function() {
            var scale = new MMap.Scale();
            map.addControl(scale);
        })
    }
    //显示地图鹰眼
    if (bOverView) {
        map.plugin(["MMap.OverView"], function() {
            var overview = new MMap.OverView();
            map.addControl(overview);
        })
    }
}
//---鼠标插件
function InitMouseTool() {
    map.plugin("MMap.MouseTool", function() {
        mouseTool = new MMap.MouseTool(map);
    });

}

//---画路径邮件保存    divId 保存按钮的Id值   url 保存路径  Direction   流向   Relation 关系
function DrawLineSave(divId, url) {
    map.bind(mouseTool, "draw", function(e) {
        pathDraw = e.getPath();
        $("#" + divId).bind('click', SaveData(url, pathDraw));
    });
}

//---创建marker---
//---lng lat 经度纬度  data 需要的数据   icon  图标  color 字体颜色  showClose 是否显示title(显示  不显示)
var arrMarker = [];

function Marker(lng, lat, title, image, color, id, showClose, x, y) {
    var marker = new MMap.Marker({
        position: new MMap.LngLat(lng, lat),
        content: createMarker(title, image, color, showClose),
        draggable: false,
        offset: {
            x: x,
            y: y
        }
    });
    arrMarker.push(marker);
    marker.id = id;
    map.addOverlays(marker);
    return marker;
}
//---marker添加事件
function markerEvent(marker, event, e) {
    map.bind(marker, event, e);
}

function updateMarker(lng, lat, image) {
    for (var i = 0; i < arrMarker.length; i++) {
        if (arrMarker[i].position.lng === lng && arrMarker[i].position.lat === lat) {
            var markeroptions = map.getOverlays(arrMarker[i].id);
            markeroptions.icon = image;
            //    markeroptions.position=new MMap.LngLat(lng,lat);    
            map.updateOverlay(markeroptions); //更新点覆盖物 
        }
    }
}

function createMarker(title, image, color, showClose) {
    var markerContent = document.createElement("div");
    markerContent.className = "markerContent";
    //图片
    var markerImg = document.createElement("img");
    markerImg.className = "markerImg";
    markerImg.src = image;
    markerContent.appendChild(markerImg);
    //文字
    if (showClose) {
        var markerSpan = document.createElement("span");
        markerSpan.style.color = color;
        markerSpan.innerHTML = title;
        markerContent.appendChild(markerSpan);
    }
    return markerContent;
}

//----画圆-----
//lng,lat 经纬度   radius,半径
var circle;

function addCircle(lng, lat, radius, fillOpacity) {
    if (circle != null) {
        circle.hide();
    }
    circle = new MMap.Circle({
        id: "circle",
        center: new MMap.LngLat(lng, lat),
        radius: radius,
        strokeColor: "#F33", //线颜色
        strokeOpacity: 0.6, //线透明度
        strokeWeight: 2, //线宽度
        fillColor: "#ee2200", //填充颜色
        fillOpacity: fillOpacity //填充透明度
    });
    map.addOverlays(circle);
    map.setFitView();
}

function clearCircle() {
    if (circle != null) {
        circle.hide();
    }
}
//----画图形
//-----路径数组，流向（1,2），关系（0,1），告警类型（0 实时站点告警 1 手工站点告警 null 无告警）
var polyline;

function addPolyline(pathArr, Direction, Relation, AlarmStatus) {
    var arr1 = new Array();
    for (var i = 0; i < pathArr.length; i++) {
        if (pathArr[i].Lgn === undefined || pathArr[i].Lat === undefined) {
            arr1.push(new MMap.LngLat(pathArr[i].lng, pathArr[i].lat));
        } else {
            arr1.push(new MMap.LngLat(pathArr[i].Lgn, pathArr[i].Lat));
        }
        //---先判断是否为双向流，如果为双向流，则不用考虑上下游设置箭头
        polyline = new MMap.Polyline({
            path: arr1, //设置线覆盖物路径
            strokeColor: "gray", //线颜色
            strokeOpacity: 1, //线透明度
            strokeWeight: 3, //线宽
            strokeStyle: "solid", //线样式
            strokeDasharray: [10, 5] //补充线样式
        });
        if (Direction === 1 && Relation === 1) {
            //单向流  下游
            polyline.icons = [{
                type: "BACKWARD_ARROW",
                offset: '25%'
            }, {
                type: "BACKWARD_ARROW",
                offset: '65%'
            }];
            if (AlarmStatus === 0) {
                polyline.strokeColor = "red";
            } else if (AlarmStatus === 1) {
                polyline.strokeColor = "#FF9900";
            }
        } else if (Direction === 2) {
            //双向流
            polyline.icons = [{
                type: "BACKWARD_ARROW",
                offset: '25%'
            }, {
                type: "FORWARD_ARROW",
                offset: '65%'
            }];
            if (AlarmStatus === 0) {
                polyline.strokeColor = "red";
            } else if (AlarmStatus === 1) {
                polyline.strokeColor = "#00CC99";
            }
        } else {
            polyline.icons = [{
                type: "FORWARD_ARROW",
                offset: '25%'
            }, {
                type: "FORWARD_ARROW",
                offset: '65%'
            }];
            if (AlarmStatus === 0) {
                polyline.strokeColor = "red";
            } else if (AlarmStatus === 1) {
                polyline.strokeColor = "#00CC99";
            }
        }
    }
    map.addOverlays(polyline);
}


function clearPolyline() {
    return polyline.setPath(null);//设置折线的经纬度坐标数组
}

//---对经纬度进行转换
function bLngLat(lng, lat) {
    return new MMap.LngLat(lng, lat);
}
//---窗体
//--title  标题  content 内容  width 窗体宽度  center 中心点  x y 偏移量 choice  是否需要头部和底部
function InfoWindow(title, content, width, lng, lat, x, y, choice, height) {
    var Center;
    if (lng === null || lat === null) {
        Center = map.getCenter();
    } else {
        Center = new MMap.LngLat(lng, lat);
    }
    var win = new MMap.InfoWindow({
        isCustom: true,
        content: createWindows(title, content, width, choice, height),
        offset: new MMap.Pixel(x, y)
    });
    map.setCenter(Center);


    return {
        winD: win,
        Center: Center
    };
}

function createWindows(title, content, width, choice, height) {
    var info = document.createElement("div");
    info.className = "infoWin";
    info.style.width = width + "px";
    if (height) {
        info.style.height = height + "px";
    }
    if (choice) {
        //---定义顶部标题
        var topTitle = document.createElement("div");
        topTitle.className = "infoWin-top";
        topTitle.style.width = width + "px";
        topTitle.style.height = "30px";
        topTitle.style.backgroundColor = "#FFF";
        var titleD = document.createElement("div");
        if (title === "undefined" || title === null || title === "") {
            title = "--";
        }
        //titleD.style.padding="0px 5px";
        titleD.style.float = "left";
        titleD.style.lineHeight = "28px";
        titleD.innerHTML = title;
        //var closeX="/Js/Map/close2.gif";

        var closeX = document.createElement("img");
        closeX.src = "http://webapi.amap.com/images/close2.gif";
        closeX.onclick = closeInfoWindow;
        closeX.style.float = "right";
        closeX.style.padding = "4px 5px"

        topTitle.appendChild(titleD);
        topTitle.appendChild(closeX);
        info.appendChild(topTitle);
        var clearF = document.createElement("div");
        clearF.style.clear = "both";
        info.appendChild(clearF);
        //---定义中部内容
        var middle = document.createElement("div");
        middle.className = "infoWin-middle";
        middle.innerHTML = content;
        info.appendChild(middle);
        //----定义底部内容
        var bottom = document.createElement("div");
        bottom.className = "infoWin-bottom";
        bottom.style.position = "relative";
        bottom.style.top = "0px";
        bottom.style.margin = "0 auto";
        var sharp = document.createElement("img");
        sharp.className="win_sharp";
        sharp.src = "../DW/images/mapwindow_sharp.png";
        bottom.appendChild(sharp);
        info.appendChild(bottom);
    } else {
        //---定义中部内容
        var middle = document.createElement("div");
        middle.className = "infoWin-middle";
        middle.innerHTML = content;
        info.appendChild(middle);
    }
    return info;

}

//构建自定义信息窗体

function closeInfoWindow() {
    map.clearInfoWindow();
}


 
//构建自定义信息窗体
function createWindow(title, content, width, backFlag) {
    var info = document.createElement("div");
    info.className = "infoWin";

    //可以通过下面的方式修改自定义窗体的宽高
    info.style.width = width + "px";

    // 定义顶部标题
    var top = document.createElement("div");
    top.className = "infoWin-top";
    var titleD = document.createElement("div");
    titleD.innerHTML = title;
    var closeX = document.createElement("img");
    closeX.src = "../DW/images/mapwindow_close2.gif";
    closeX.onclick = closeInfoWindow;
    top.appendChild(titleD);
    top.appendChild(closeX);
    info.appendChild(top);
    // 定义中部内容
    var middle = document.createElement("div");
    middle.className = "infoWin-middle";
    if (backFlag) {
        middle.style.backgroundImage = 'url(../DW/images/mapwindow_bg.png)';
    }
    middle.innerHTML = content;
    info.appendChild(middle);

    // 定义底部内容
     var bottom = document.createElement("div");
     bottom.className = "infoWin-bottom";
     bottom.style.position = 'relative';
     bottom.style.top = '0px';
     bottom.style.margin = '0 auto';
     var sharp = document.createElement("img");
    sharp.src = "../DW/images/mapwindow_sharp.png";
    bottom.appendChild(sharp);
     info.appendChild(bottom);

    return info;
}


function createMarkerImg(title,content,image,level) {
    var markerContent = document.createElement("div");
    markerContent.className = "markerImgStyle";
    var markerImg = document.createElement("img");
    markerImg.src = image;
    markerContent.appendChild(markerImg);
    //点标记中的文本  
    var markerSpan = document.createElement("span");
    if (level <= 2) {
        markerSpan.style.color = 'Black';
    } else {
        markerSpan.style.color = 'Blue';
    }
     markerSpan.innerHTML = content;
    markerContent.appendChild(markerSpan);
    return markerContent;
}

